package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.ErrorCode
import com.hzjq.core.callback.Callback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class UpgradeInnerModeWork : Work<Int> {

    private var count = 0
    private var mDisposable: Disposable? = null
    private var innerMode = false


    constructor(callback: Callback<Int>) : super(callback)


    override fun doWork(vararg args: Any) {
        innerMode = false
        innerUpgradeMode()
    }


    /**
     * 进入升级模式
     */
    private fun innerUpgradeMode() {
        Receives.getInstance().registerReceiver(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .getInnerUpgradeModeCmd(), object : Receiver {
                override fun convert(msg: String): Any {
                    return BlastDelegate.getDelegate().getParseLoader().parseUpgradeModeMsg(msg)
                }

                override fun onSuccess(msg: Any) {
                    val success = msg as Boolean
                    if (success) {
                        stopRetry()
                        onAddProgressChanged(5, 100, "进入升级模式成功")
                        if (!innerMode) {
                            innerMode = true
                            doNext()
                        }

                    } else {
                        if (count > 0) {
                            failed()
                        } else {
                            count++
                            delayRetry()
                        }
                    }
                }

                override fun failed() {
                    stopRetry()
                    onProgressChanged(100, "进入升级模式失败")
                    callback?.onError(ErrorCode.getErrorResult(-23))
                    onDestroy()
                }
            })
        val msg = DataMessageBean(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .getInnerUpgradeModeCmd().cmd
        )
        BlastDelegate.getDelegate().getCmdExeLoader()
            .exePollResultCmd(msg.assembly(), callback)
    }

    private fun stopRetry() {
        if (mDisposable != null && !mDisposable!!.isDisposed) {
            mDisposable?.dispose()
        }
    }


    private fun delayRetry() {
        stopRetry()
        mDisposable = Observable.timer(100, TimeUnit.MILLISECONDS, Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.newThread())
            .subscribe {
                innerUpgradeMode()
            }
    }


    override fun cancel() {
        if (mDisposable != null && !mDisposable!!.isDisposed) {
            mDisposable?.dispose()
        }
        Receives.getInstance().unRegisterReceiver(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .getInnerUpgradeModeCmd()
        )
    }
}